package st.lib.base.net;

import android.content.Context;

import java.io.File;
import java.util.WeakHashMap;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import st.lib.base.net.callback.IError;
import st.lib.base.net.callback.IFailure;
import st.lib.base.net.callback.IRequest;
import st.lib.base.net.callback.ISuccess;
import st.lib.base.net.loader.LoaderStyle;

/**
 * Created by zhangyu on 2017/10/19.
 */

public class RestClintBuilder {
    private String mUrl;
    private final WeakHashMap<String, Object> PARAMS = new WeakHashMap<>();
    private IRequest mRequest;
    private ISuccess mSuccess;
    private IFailure mFailure;
    private IError mError;
    private RequestBody mBody;
    private LoaderStyle mLoaderStyle;
    private Context mContext;
    private File mFile;
    private String mDownloadDir;
    private String mExtension;
    private String mName;

    RestClintBuilder() {

    }

    public final RestClintBuilder url(String url) {
        this.mUrl = url;
        return this;
    }

    public final RestClintBuilder params(WeakHashMap<String, Object> params) {
        PARAMS.putAll(params);
        return this;
    }

    public final RestClintBuilder params(String key, Object value) {
        PARAMS.put(key, value);
        return this;
    }

    public final RestClintBuilder request(IRequest request) {
        this.mRequest = request;
        return this;
    }

    public final RestClintBuilder success(ISuccess success) {
        this.mSuccess = success;
        return this;
    }

    public final RestClintBuilder failure(IFailure failure) {
        this.mFailure = failure;
        return this;
    }

    public final RestClintBuilder error(IError error) {
        this.mError = error;
        return this;
    }

    public final RestClintBuilder raw(String raw) {
        this.mBody = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), raw);
        return this;
    }

    public final RestClintBuilder loader(Context context, LoaderStyle style) {
        mContext = context;
        mLoaderStyle = style;
        return this;
    }

    public final RestClintBuilder loader(Context context) {
        mContext = context;
        mLoaderStyle = LoaderStyle.BallSpinFadeLoaderIndicator;
        return this;
    }

    public final RestClintBuilder file(File file) {
        mFile = file;
        return this;
    }

    public final RestClintBuilder file(String file) {
        mFile = new File(file);
        return this;
    }

    public final RestClintBuilder downloadDir(String downloadDir) {
        mDownloadDir = downloadDir;
        return this;
    }

    public final RestClintBuilder extension(String extension) {
        mExtension = extension;
        return this;
    }

    public final RestClintBuilder name(String name) {
        mName = name;
        return this;
    }

    public final RestClintBuilder body(RequestBody body) {
        mBody = body;
        return this;
    }

    public final RestClient build() {
        return new RestClient(mUrl, PARAMS, mRequest,
                mSuccess, mFailure,
                mError, mBody,
                mLoaderStyle,
                mContext, mFile,
                mDownloadDir,
                mExtension,
                mName);
    }
}
